<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * create email-invoice and returned it as string
 * for email-function
 * get: invoice.mail.tpl.php
 * used from cart.save.php
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */
function makeInvoice($cid, $attachements = FALSE)
{
	$mail = _('No Mailtemplate found!');
	$file = 'template/mail.form/invoice.mail.tpl.php';
	if (is_file($file))
	{
		$mail = file_get_contents($file);
		$parts = explode('?>', $mail);
		$mail = $parts[1];
		// include shopadress
		$search = array('{companyname}', '{companystreet}', '{companycity}',
		 '{companyphone}', '{companyfax}', '{companyemail}', '{logo}');
		$value = array(COMPANY_NAME, COMPANY_STREET, COMPANY_CITY,
		 COMPANY_PHONE, COMPANY_FAX, COMPANY_EMAIL, '<img src="cid:' . $cid . '" />');
		$mail = str_replace($search, $value, $mail);

		// include adressfield
		$search = array('{titel}', '{company}',
		 '{name}',
		 '{street}', '{city}');
		db::getInstance()->where('orderer_id', $_SESSION['ordererIdx']);
		$result = db::getInstance()->getOne('orderer');

		$value = array(_('Invoice'), $result['orderer_company'],
		 $result['orderer_firstname'] . ' ' . $result['orderer_lastname'],
		 $result['orderer_street'], $result['orderer_zip'] . ' ' . $result['orderer_city']);
		$mail = str_replace($search, $value, $mail);

		$mail = str_replace('{ordernumber}', $_SESSION['ordernumberx'], $mail);

		// Productlist
		$parts = explode('<!-- BEGIN productList -->', $mail);
		$mail = $parts[0];
		$buffer = $parts[1];
		$parts = explode('<!-- END productList -->', $buffer);

		foreach ($_SESSION['productListx'] as $product)
		{
			$search = array('{sku}', '{product}', '{pricePerItem}',
			 '{number}', '{priceTotal}');
			$value = array($product['sku'], $product['product'], $product['pricePerItem'],
			 $product['number'], $product['priceTotal']);
			$mail .= str_replace($search, $value, $parts[0]);
			// TODO: Include Attributes
		}
		$mail .= $parts[1];

		// Price/Tax
		$search = array('{price}', '{shippingPrice}', '{shippingComment}');
		$value = array($_SESSION['totalpricex'], $_SESSION['shippingtotalx'],
		 $_SESSION['shippingcommentx']);
		$mail = str_replace($search, $value, $mail);

		$parts = explode('<!-- BEGIN tax -->', $mail);
		$mail = $parts[0];
		$buffer = $parts[1];
		$parts = explode('<!-- END tax -->', $buffer);

		foreach ($_SESSION['ordertaxx'] as $tax)
		{
			$search = array('{taxname}', '{percent}', '{value}');
			$value = array($tax['name'], $tax['percent'], ($tax['value'] . CURRENCY));
			$mail .= str_replace($search, $value, $parts[0]);
		}
		$mail .= $parts[1];

		// Comment...
		db::getInstance()->where('payment_name', $_SESSION['paymentx']);
		$payment = db::getInstance()->getOne('payment');

		$_SESSION['paymentidx'] = $payment['payment_id'];
		$pagefile = 'plugins/payment/' . $payment['payment_file'] . '.payment.php';
		$sFunctionComment = $payment['payment_file'] . '_comment';

		if (is_file($pagefile))
			include_once($pagefile);
		$buffer = $sFunctionComment($payment['payment_id']);

		$search = array('{comment}', '{payment}', '{footer}');

		db::getInstance()->where('page_name', 'mail-footer');
		$row = db::getInstance()->getOne('page');

		$value = array($_SESSION['commentx'], $buffer, $row['page_text']);
		$mail = str_replace($search, $value, $mail);

		// revocation and/or terms
		$revocation = '';
		$terms = '';
		if ($attachements)
		{
			if (Mail_SEND_REVOCATION)
			{
				db::getInstance()->where('page_name', 'mail-revocation');
				$row = db::getInstance()->getOne('page');
				$revocation = $row['page_text'];
			}

			if (MAIL_SEND_TERMS)
			{
				db::getInstance()->where('page_name', 'mail-terms');
				$row = db::getInstance()->getOne('page');
				$terms = $row['page_text'];
			}
		}
		$search = array('{revocation}', '{terms}');
		$value = array($revocation, $terms);
		$mail = str_replace($search, $value, $mail);

		return $mail;
	}
	else
	{
		return _('Could not load Email Template!');
	}
}

?>